//index.js
//获取应用实例
const app = getApp()
Date.prototype.Format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1, //月份 
    "d+": this.getDate(), //日 
    "H+": this.getHours(), //小时 
  };
  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
}

Page({
  data: {
    checkType:'',
    checkCategory:'',
    checkDate:'',
    checkContent:'',
    content:'',
    index: null,
    problemCategory1: ['违章', '隐患'],
    problemCategory:'',
    address:'',
    companyCode:'',
    companyName:'',
    checkListItemCode:"",
    checkListItemName:"",
    problemDescription:"",
    checkAttach1:'',
    checkAttach2:'',
    checkAttach3:'',
    checkAttach4:'',
    isCheck:'',
    isRectOntime:'',
    isRules:'',
    isTrain:'',
    limtRectDate:'2019-05-25',
    status:'未审核',
    parentCompanyName:'西南分公司',
    // checkDate: new Date().Format("yyyy-MM-dd"),
    checkDate:'',
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    motto: 'Hi 开发者！',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),

    
    // region: ['广东省', '广州市', '海珠区'],
    multiIndex: [0, 0, 0],
    imgList: [],
    imgList1: [],
    imgList2: [],
    imgList3: [],
    multiIndex: [0, 0, 0],
    unnormalizedValue2:[],
    attach:[],
    isRuleTrue1:'',
    isRuleTrue2: '',
    isRuleTrue3: '',
    isRuleTrue4: '',
    
  },
  //选择检查类型
  PickerChange(e) {
    var that = this
    console.log(e);
    this.setData({
      index: e.detail.value,
    })
    that.data.problemCategory = that.data.problemCategory1[e.detail.value]
  },
  showRule: function () {
    this.setData({
      isRuleTrue: true
    })
  },
  hideRule: function () {
    this.setData({
      isRuleTrue: false
    })
  },
  selThis(e) {
    this.setData({
      companyName: e.detail.label,
      companyCode: e.detail.nodeCode,
      isRuleTrue: false
    })
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  
  onLoad: function(options) {
    var company_list=[]
    var that=this;
    var checkType=options.checkType;
    var checkCategory=options.checkCategory;
    var checkDate=options.checkDate;
    var checkContent=options.checkContent;
    var content=options.content;
    var companyCode=options.companyCode;
    var companyName=options.companyName;
    // var checkDate=options.checkDate;
    var checkListItemName=options.checkItemName3;
    var checkListItemCode=options.checkItemCode;
    that.setData({
      companyCode:companyCode,
      companyName:companyName,
      // checkDate:checkDate,
      checkListItemName:checkListItemName,
      checkListItemCode:checkListItemCode,
      checkType:checkType,
      checkCategory:checkCategory,
      checkDate:checkDate,
      checkContent:checkContent,
      content:content
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    //获取单位列表
    wx.request({
      url: 'http://39.98.173.131:7000/wlhse/api/v3/company/tree',
      data: {},
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Authorization': wx.getStorageSync("token")//读取cookie
      },
      success: function (res) {
        company_list = res.data.data
        // console.log(JSON.stringify(res))
        that.setData({
          unnormalizedValue2: company_list
        });
      },
      fail: function (err) {
        //失败执行
      }
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  DateChange(e) {
    this.setData({
      checkDate: e.detail.value
    })
  },
  //输入问题详细描述
  descriptionDetail: function (e) {
    this.setData({
      problemDescription: e.detail.value
    })
  },
  //是否有制度
  isRules:function(e){
    var that = this;
    this.setData({
      isRules: e.detail.value
    })
    if (e.detail.value === true) {
      that.data.isRules="是"
      this.setData({
        isRuleHide1: true
      })
    } else {
      that.data.isRules= "否"
      this.setData({
        isRuleHide1: false
      })
    }
    // console.log(that.data.isRuleTrue1)
  },
  //是否有培训
  isTrain: function (e) {
    var that = this;
    this.setData({
      isTrain: e.detail.value
    })
    if (e.detail.value === true) {
      that.data.isTrain = "是"
      this.setData({
        isRuleHide2: true
      })
    } else {
      that.data.isTrain = "否"
      this.setData({
        isRuleHide2: false
      })
    }
  },
  //是否有考核
  isCheck: function (e) {
    var that = this;
    this.setData({
      isCheck: e.detail.value
    })
    if (e.detail.value === true) {
      that.data.isCheck = "是"
      this.setData({
        isRuleHide3: true
      })
    } else {
      that.data.isCheck = "否"
      this.setData({
        isRuleHide3: false
      })
    }
  },
  //提交表格
  submitClick: function () { 
    var that = this;
    wx.request({
      url: 'http://39.98.173.131:7000/wlhse/api/v3/safety_problem_add',
      method: "POST",
      data: {
        checkType:that.data.checkType,
        checkCategory:that.data.checkCategory,
        checkDate:that.data.checkDate,
        checkContent:that.data.checkContent,
        content:that.data.content,
        problemCategory:that.data.problemCategory,
        address: that.data.address,
        companyCode: that.data.companyCode,
        companyName: that.data.companyName,
        checkListItemName: that.data.checkListItemName,
        checkListItemCode: that.data.checkListItemCode,
        problemDescription: that.data.problemDescription,
        checkAttach1: that.data.checkAttach1,
        checkAttach2: that.data.checkAttach2,
        checkAttach3: that.data.checkAttach3,
        checkAttach4: that.data.checkAttach4,
        checkDate: that.data.checkDate,
        isCheck: that.data.isCheck,
        isRectOntime: that.data.isRectOntime,
        isRules: that.data.isRules,
        isTrain: that.data.isTrain,
        limtRectDate: that.data.limtRectDate,
        parentCompanyName: that.data.parentCompanyName,
        status: that.data.status,
        checkStaffID: that.data.checkStaffID
      },
      header: {
        'content-type': 'application/json' ,// 默认值
        'Authorization': wx.getStorageSync("token")//读取cookie
      },
      success: function (res) {
        console.log(res.data)
      },
    })
    wx.showToast({
      title: '提交成功',
      icon: 'success',
      duration: 2000
    })
    wx.navigateBack({
      delta: 1
 })
  },
  // RegionChange: function(e) {
  //   this.setData({
  //     region: e.detail.value
  //   })
  // },
  ChooseImage() {
    wx.chooseImage({
      count: 4, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
      success: (res) => {
        if (this.data.imgList.length != 0) {
          this.setData({
            imgList: this.data.imgList.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList: res.tempFilePaths
          })
        }
        // console.log(this.data.imgList)
      }
    });
  },
  ViewImage(e) {
    wx.previewImage({
      urls: this.data.imgList,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg(e) {
    wx.showModal({
      title: '删除照片',
      content: '确定要删除这张照片？',
      cancelText: '再看看',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {

          this.data.imgList.splice(e.currentTarget.dataset.index, 1);
          // this.data.attach.splice(e.currentTarget.dataset.index, 1)
          this.setData({
            imgList: this.data.imgList,
            // attach: this.data.attach
          })

        }
      }
    })
  },
//提交上传问题图片
  submitPicture:function(){
    var imgsurls = this.data.imgList;
    var that=this;
    if (imgsurls.length>0){
      for (var i = 0; i < imgsurls.length;i++){
        wx.uploadFile({
          url: 'http://39.98.173.131:7000/wlhse/api/v3/file_upload',
          filePath: imgsurls[i],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success(res) {
            var obj = JSON.parse(res.data)
            that.data.attach.push(obj.data)
            console.log(obj)
            console.log("attach"+that.data.attach)
            that.data.checkAttach1 = that.data.attach[0]
            that.data.checkAttach2 = that.data.attach[1]
            that.data.checkAttach3 = that.data.attach[2]
            that.data.checkAttach4= that.data.attach[3]
            console.log("checkAttach1:" + that.data.checkAttach1)
            console.log("checkAttach2:" + that.data.checkAttach2)
            console.log("checkAttach3:" + that.data.checkAttach3)
            console.log("checkAttach4:" + that.data.checkAttach4)
          }
        })
      }
    }
    wx.showToast({
      title: '上传成功',
      icon: 'success',
      duration: 2000
    })

   },

  
  ChooseImage1() {
    wx.chooseImage({
      count: 4, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
      success: (res) => {
        if (this.data.imgList1.length != 0) {
          this.setData({
            imgList1: this.data.imgList1.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList1: res.tempFilePaths
          })
        }
      }
    });
  },
  ViewImage1(e) {
    wx.previewImage({
      urls: this.data.imgList1,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg1(e) {
    wx.showModal({
      title: '删除照片',
      content: '确定要删除这张照片？',
      cancelText: '再看看',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList1.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList1: this.data.imgList1
          })
        }
      }
    })
  },
  ChooseImage2() {
    wx.chooseImage({
      count: 4, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
      success: (res) => {
        if (this.data.imgList2.length != 0) {
          this.setData({
            imgList2: this.data.imgList2.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList2: res.tempFilePaths
          })
        }
      }
    });
  },
  ViewImage2(e) {
    wx.previewImage({
      urls: this.data.imgList2,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg2(e) {
    wx.showModal({
      title: '删除照片',
      content: '确定要删除这张照片？',
      cancelText: '再看看',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList2.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList2: this.data.imgList2
          })
        }
      }
    })
  },
  ChooseImage3() {
    wx.chooseImage({
      count: 4, //默认9
      sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], //从相册选择
      success: (res) => {
        if (this.data.imgList3.length != 0) {
          this.setData({
            imgList3: this.data.imgList3.concat(res.tempFilePaths)
          })
        } else {
          this.setData({
            imgList3: res.tempFilePaths
          })
        }
      }
    });
  },
  ViewImage3(e) {
    wx.previewImage({
      urls: this.data.imgList3,
      current: e.currentTarget.dataset.url
    });
  },
  DelImg3(e) {
    wx.showModal({
      title: '删除照片',
      content: '确定要删除这张照片？',
      cancelText: '再看看',
      confirmText: '确定',
      success: res => {
        if (res.confirm) {
          this.data.imgList3.splice(e.currentTarget.dataset.index, 1);
          this.setData({
            imgList3: this.data.imgList3
          })
        }
      }
    })
  },
  textareaBInput(e) {
    this.setData({
      textareaBValue: e.detail.value
    })
  }

})